package vmo;

import java.nio.ByteBuffer;

import shared.Vec2f;

public class Actor_PlayerGhost extends Actor_Player{

	byte [] m_tape;
	int m_read,m_toread;
	Vec2f [] m_priors;
	void Init()
	{
		m_priors=new Vec2f[2];
		m_priors[0]=new Vec2f(m_data.m_pos.x,m_data.m_pos.y);
		m_priors[1]=new Vec2f(m_data.m_pos.x,m_data.m_pos.y);		
		m_tape=new byte[24];
		for (int i=0;i<24;i++)
		{
			m_tape[i]=0;
		}
		m_read=0;
		m_toread=0;	
	}
	
	public Actor_PlayerGhost (ByteBuffer buffer)
	{


		m_data=new PlayerData();
		//read position
		m_data.m_pos=new Vec2f(buffer.getFloat(),buffer.getFloat());
		//read layer, entity, uid
		m_data.m_layer=buffer.get();
		m_data.m_entity=buffer.getShort();
		m_data.m_UID=buffer.getShort();
		//read gender, facing, hairstyle, skincolour, haircolour		
		m_data.m_gender=buffer.get();
		m_data.m_facing=buffer.get();
		m_data.m_hairstyle=buffer.get();
		m_data.m_skincolour=buffer.get();
		m_data.m_haircolour=buffer.get();
		
		m_visible=true;
		Init();
	}	
	
	public Actor_PlayerGhost(PlayerData data) 
	{

		super(data);
		Init();
	}
	
	public void LoadRecord(int offset, byte [] array)
	{	
		

		int startpoint=0;
		int writepoint=m_read+1;
		int towrite=12;
		//skip frames
		if (offset<=0)
		{
			startpoint=offset*-1;
			
			
		}
		if (offset>=0)
		{
			//offset recording into the future.
			writepoint+=offset;
			if (writepoint>=24)
			{
				writepoint=writepoint-24;
			}
			//towrite-=offset;
		}
		System.out.println(offset);
		for (int i=startpoint;i<towrite;i++)
		{

			
			m_tape[writepoint]=array[i];
			writepoint++;
			if (writepoint>=24)
			{
				writepoint=0;
			}
		}

	}

	
	@Override	
	public void LoadRecord(int offset, byte [] array, Vec2f p, int z)
	{
	
		System.out.println();
		
		if (offset>0)
		{
			offset=0;
		}
		if (offset<-12)
		{
			offset=-12;
		}
		setPos(p,(byte) z);
		if (m_rui!=null)
		{
			m_rui.RePosition();
		}
		int rewind=offset*-1;
		for (int i=0;i<rewind;i++)
		{
			byte a=(byte) (array[i] & 0b1111);
			byte d=(byte) (array[i]>>4);
			if (a!=0)
			{
				Action(a,d,false);
			}
		}
	
		m_toread=12-rewind;
		
		int writepos=m_read;
		if (writepos>=12)
		{
			writepos=0;
		}	
		for (int i=rewind;i<12;i++)
		{
			int v=i;
			if (v>=12)
			{
				v=v-12;
			}
			m_tape[writepos]=array[v];
			
			writepos++;
			if (writepos>=12)
			{
				writepos=0;
			}
		}
		
	}
	
	@Override
	public void Update(float dt) {
		

			byte a=(byte) (m_tape[m_read] & 0b1111);
			byte d=(byte) ((m_tape[m_read] & 0b11110000)>>4);

			Action(a,d,false);

			m_tape[m_read]=0;
			m_read++;
			if (m_read==24)
			{
				m_read=0;
			}
	}	
	
	@Override
	public short getUID() {
		// later, reset this
		return (short) (m_data.m_UID+1);
	}	

	@Override	
	void setLayer(short l)
	{
		m_data.m_layer=l;
	}
	
	@Override
	public void Encode(ByteBuffer buffer) {
		// TODO Auto-generated method stub
		
		//write position
		buffer.putFloat(m_data.m_pos.x);
		buffer.putFloat(m_data.m_pos.y);
		//write layer, entity, uid
		buffer.put((byte) m_data.m_layer);
		buffer.putShort(m_data.m_entity);
		buffer.putShort(m_data.m_UID);
		//write gender, facing, hairstyle, skincolour, haircolour
		buffer.put(m_data.m_gender);
		buffer.put(m_data.m_facing);
		buffer.put(m_data.m_hairstyle);
		buffer.put(m_data.m_skincolour);
		buffer.put(m_data.m_haircolour);
		
	}		

	@Override
	public Vec2f getPriorPos(int v) {
		// TODO Auto-generated method stub
		return m_priors[v];
	}	
	
	@Override
	public void setPrior() {
		// TODO Auto-generated method stub
		m_priors[0]=new Vec2f(getPosition().x,getPosition().y);
		m_priors[1]=m_priors[0];
	}		
	
}


